<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>用户管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link media="all" rel="stylesheet" th:href="@{/static/layuiadmin/layui/css/layui.css}">
    <link media="all" rel="stylesheet" th:href="@{/static/layuiadmin/style/admin.css}">
    <style>
        .form-label {
            width: 150px;
        }

        .form-input {
            width: 300px;
        }
    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md6">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <!--                    <label class="layui-form-label">用户名</label>-->
                                    <input type="text" name="tableName" placeholder="表名" autocomplete="off"
                                           class="layui-input">
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn" lay-submit lay-filter="search" id="search">搜索</button>
                                </div>
                            </div>
                        </div>
                        <div class="layui-card-body">
                            <table id="item-list" lay-filter="item-list"></table>

                            <script type="text/html" id="item-bar">
                                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="doGenerator"><i
                                        class="layui-icon layui-icon-edit"></i>生成到目录</a>
                            </script>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-col-md6">
            <div class="layui-card">
                <div class="layui-card-header">生成代码配置</div>
                <div class="layui-form layui-card-body" style="">
                    <div class="layui-form-item">
                        <label class="layui-form-label form-label">作者：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="author" autocomplete="off"
                                   class="layui-input form-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label form-label">基础包名：</label>
                        <div class="layui-input-inline">
                            <input id="name" name="basePackage" class="layui-input form-input"
                                   lay-verify="required" autocomplete="off" type="text"/>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label form-label">controller包名：</label>
                        <div class="layui-input-inline form-input">
                            <input name="mapperXmlPackage" class="layui-input form-input"
                                   lay-verify="required" autocomplete="off" type="text"/>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label form-label">service包名：</label>
                        <div class="layui-input-inline form-input">
                            <input name="servicePackage" class="layui-input form-input"
                                   lay-verify="required" autocomplete="off" type="text"/>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label form-label">serviceImpl包名：</label>
                        <div class="layui-input-inline form-input">
                            <input name="serviceImplPackage" class="layui-input form-input"
                                   lay-verify="required" autocomplete="off" type="text"/>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label form-label">mapper包名：</label>
                        <div class="layui-input-inline form-input">
                            <input name="mapperPackage" class="layui-input form-input" lay-verify="required"
                                   autocomplete="off" type="text"/>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label form-label">model包名：</label>
                        <div class="layui-input-inline">
                            <input name="entityPackage" class="layui-input form-input"
                                   lay-verify="required" autocomplete="off" type="text"/>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label form-label">mapperXml包名：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="mapperXmlPackage" autocomplete="off"
                                   maxlength="20" lay-verify="required" class="layui-input form-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label form-label">生成路径配置：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="genPath" autocomplete="off"
                                   maxlength="50" lay-verify="required" class="layui-input form-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label form-label">是否去除表前缀：</label>
                        <div class="layui-input-block">
                            <input type="radio" name="isTrim" value="1" lay-filter="isTrim" title="是">
                            <input type="radio" name="isTrim" value="0" lay-filter="isTrim" title="否">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label form-label">表前缀：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="trimValue" autocomplete="off" maxlength="20"
                                   class="layui-input form-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-inline" style="padding: 20px 0 20px 180px">
                            <button class="layui-btn" lay-submit="" lay-filter="submit-btn">保存配置</button>
                        </div>
                    </div>
                    <div class="layui-form-item layui-hide">
                        <input type="text" name="id" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script th:src="@{/static/layuiadmin/layui/layui.js}"></script>
<script th:src="@{/static/bm/js/common.js}"></script>
<script>
    layui.config({
        base: contextPath + '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'form'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table;

        //监听搜索
        form.on('submit(search)', function (data) {
            table.reload('item-list', {
                where: data.field
            });
        });

        table.render({
            elem: '#item-list'
            , url: contextPath + '/generator/list'
            , page: true
            , toolbar: false
            , limit: 10 //默认十条数据一页
            , limits: [10, 20, 30, 50] //数据分页条
            , cols: [
                [ //表头
                    {field: 'id', title: 'ID', align: 'center', hide: true},
                    {field: 'name', title: '表名', align: 'center'},
                    {field: 'remark', title: '备注', align: 'center'},
                    {fixed: 'right', title: '操作', align: 'center', toolbar: '#item-bar'}
                ]
            ]
        });

        //监听工具条
        table.on('tool(item-list)', function (obj) {
            var data = obj.data;
            if (obj.event === 'doGenerator') {
                var loading = layer.load();
                $.post(contextPath + "/generator/gen", data, function (ev) {
                    layer.close(loading);
                    console.log(ev);
                    if (ev.code === '0000') {
                        layer.alert(ev.msg + "，生成路径为：" + JSON.stringify(ev.data));
                    } else {
                        layer.msg(ev.msg);
                    }
                });
            }
        });


        //配置详情
        $.post(contextPath + '/generator/config/detail', function (ev) {
            if (ev.code === '0000') {
                var obj = ev.data;
                Object.keys(obj).forEach(function (key) {
                    if (key === "isTrim") {
                        $("input[name=isTrim][value='0']").attr("checked", obj[key] === '0');
                        $("input[name=isTrim][value='1']").attr("checked", obj[key] === '1');
                    } else {
                        $("input[name=" + key + "]").val(obj[key] || "");
                    }
                });
                form.render();
            }
        });

        //修改配置
        form.on('submit(submit-btn)', function (data) {
            layer.confirm('确认提交吗？', function () {
                var loading = layer.load();
                $.post(contextPath + '/generator/config/update', data.field, function (ev) {
                    layer.close(loading);
                    if (ev.code === '0000') {
                        layer.msg(ev.msg, {
                            icon: 6, shade: 0.3, time: 1000,
                        });
                    } else {
                        layer.msg(ev.msg);
                    }
                }, 'json');
            });
        });
    });
</script>
</body>
</html>
